package com.youfan.control;

import com.youfan.entity.ProductattributeValue;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "productAttr/productAttributeValue")
public class ProductAttributeValueControl {

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody ProductattributeValue youfanproductattributeValuet)
    {
        try {
            int sucessnums = productAttrValueService.insertOne(youfanproductattributeValuet);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody ProductattributeValue youfanproductattributeValue){
        try {
            if (youfanproductattributeValue.getPageNum() == null) {
                youfanproductattributeValue.setPageNum(1);
            }
            if (youfanproductattributeValue.getPageSize() == null) {
                youfanproductattributeValue.setPageSize(5);
            }
            CommonPage<ProductattributeValue> resultdata = productAttrValueService.queryByCondtion(youfanproductattributeValue);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody ProductattributeValue youfanproductattributeValue)
    {
        try {
            ProductattributeValue youfanproductattributeValue1 = productAttrValueService.findByid(youfanproductattributeValue);
            return Result.success(youfanproductattributeValue1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody ProductattributeValue youfanproductattributeValue)
    {
        try {

            ProductattributeValue youfanproductattributeValue1 = productAttrValueService.findByid(youfanproductattributeValue);
//            BeanUtils.copyProperties(youfanproductattributeDict,youfanproductattributeDict1);
            youfanproductattributeValue1.setAttributeValue(youfanproductattributeValue.getAttributeValue());
            int result = productAttrValueService.updateOne(youfanproductattributeValue1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
